/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package client.dm;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.MediaTracker;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class GUI implements ActionListener, KeyListener, WindowFocusListener{
    
    private windowTablero mainChessBoard;
    private objCreateAppletImage createImage;
    private JButton pedirFicha, pasar, nuevoJuego;
    private JLabel jugador, comp0, comp1, comp2, comp3, comp4, comp5, comp6, comp7, comun;
    private MediaTracker mt;
    private Escucha es;
    private Conexion cnx;
    
    public void GUI() {
        comp0.setText(""); 
        comp1.setText(""); 
        comp2.setText(""); 
        comp3.setText(""); 
        comp4.setText(""); 
        comp5.setText(""); 
        comp6.setText("");  
        comp7.setText(""); 
        comun.setText("Tren");
    }

    public void setJugador(String jug) {
        jugador = new JLabel("    ", JLabel.RIGHT);
        this.jugador.setText(jug);        
    }

    public JButton getNuevoJuego() {
        return nuevoJuego;
    }
        
    public Container crearGUI(JFrame mainApp) {
        
        JPanel panRoot = new JPanel(new BorderLayout());
        panRoot.setOpaque(true);
        panRoot.setPreferredSize(new Dimension(650,700));
                
        mainChessBoard = new windowTablero();
        createImage = new objCreateAppletImage();
        
        mainChessBoard.setSize(new Dimension(600, 500));
        mainChessBoard.setNames(jugador.getText(), 0);
        
        pedirFicha = new JButton("Pedir ficha");
        pasar = new JButton("Pasar - Marcar");
        nuevoJuego = new JButton("JUEGO NUEVO");
        
        pedirFicha.addActionListener(this);
        pasar.addActionListener(this);
        nuevoJuego.addActionListener(this); 
        
        mainChessBoard.setEnabled(false);
        try {
            cnx = new Conexion();
            es = new Escucha(cnx,mainChessBoard, this);
            es.start();
            
            mainChessBoard.setHilo(es);
            pedirFicha.setVisible(false);
            pasar.setVisible(false);
            nuevoJuego.setVisible(false);
            
            mt = new MediaTracker(mainApp);
            
        } catch (NullPointerException e) {
            
            JOptionPane.showMessageDialog(null, "Imposible cargar las imagenes.", "ERROR", JOptionPane.WARNING_MESSAGE);
            nuevoJuego.setEnabled(false);            
            e.printStackTrace();
            
        }
        panRoot.add(mainChessBoard, BorderLayout.CENTER);
      
        JPanel panel = new JPanel(new GridLayout(1,3,2,2));
        
        panel.add(jugador);
        panel.add(pedirFicha);
        panel.add(pasar);
        panel.add(nuevoJuego);
        panel.setBackground(Color.WHITE);
        panRoot.add(panel,BorderLayout.SOUTH);
        
        /*
        JPanel panelL = new JPanel(new GridLayout(9,1,2,2));
        panelL.add(comun);
        panelL.add(comp0);
        panelL.add(comp1);
        panelL.add(comp2);
        panelL.add(comp3);
        panelL.add(comp4);
        panelL.add(comp5);
        panelL.add(comp6);
        panelL.add(comp7);
        panRoot.add(panel,BorderLayout.WEST);
        */
        panRoot.setBackground(Color.ORANGE);
                
        jugador.setBackground(new Color(236,17,17));        
              
        return panRoot;
    }
    
    public void actionPerformed(ActionEvent e) {
        
        if (e.getSource() == nuevoJuego) {
            pedirFicha.setVisible(true);
            pasar.setVisible(true);
            nuevoJuego.setEnabled(false);            
            mainChessBoard.setNombreJugador(jugador.getText());
            mainChessBoard.setEnabled(true);
            mainChessBoard.newGame();
        }
        
        if (e.getSource() == pasar) {
            mainChessBoard.pasar();
        }
        
        if (e.getSource() == pedirFicha) {
            mainChessBoard.pedirFicha();
        }
    }
    
    public void keyPressed(KeyEvent e) {
    }
    
    public void keyReleased(KeyEvent e) {
    }
    
    public void windowGainedFocus(WindowEvent e) {
        mainChessBoard.gotFocus();
    }
    
    public void windowLostFocus(WindowEvent e) {
    }

    @Override
    public void keyTyped(KeyEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
